<?php

namespace app\api\controller;

use think\Cache;
use app\admin\logic\User;
use app\admin\model\Users as Users_model;

/**
 * Class Client
 * RPC通信用户中心
 * @package app\api\controller
 * @author: 蒋
 */
class Client
{
    private $url = 'http://center.121314.com/api/Servers/';

    /**
     * 通信接口
     * @param $method 要访问的接口类型
     * @param $fun 方法名称
     * @param string $data 参数
     * @return mixed
     * @author: Huang
     */
    public function Client($method, $fun, $data = '')
    {
        $url = $this->url . $method;
        $client = new \Hprose\Http\Client($url, false);
        $userService = $client->useService();
        $data['authkey'] = 'jinfuren';
        $data['site_id'] = config('admin.site_id');
        $result = $userService->$fun($data);
        return $result;
    }
    /**
     * 获取所有的用户信息
     * @param bool $force
     * @return mixed
     * @author: Huang
     */
    public function getAllUserInfo($force = false)
    {
        $alluserinfo = Cache::tag('alluser')->get('alluserinfo');
        if (empty($alluserinfo) || $force) {
            $alluserinfo = Users_model::all();
            Cache::tag('alluser')->set('alluserinfo', $alluserinfo);
        }
        return $alluserinfo;
    }

    /**
     * 根据id来获取用户信息
     * @param $uid
     * @param bool $force
     * @return mixed
     * @author: Huang
     */
    public function getOneUserById($uid, $force = false)
    {
        return collect($this->getAllUserInfo($force))->where('id', $uid)->first();
    }

    /**
     * 获取所有的部门信息
     * @param bool $force
     * @return mixed
     * @author: Huang
     */
    public function getAllDepar($force = false)
    {
        $alluserinfo = Cache::tag('allDepar')->get('alluserdepar');
        if (empty($alluserinfo) || $force) {
            $alluserinfo = $this->Client('depar', 'getAllDepars');
            Cache::tag('allDepar')->set('alluserdepar', $alluserinfo);
        }
        return $alluserinfo;
    }

}
